<template>
	<view class="shop" :style="{background:pageColor}" v-if="isLoad">
		<view class="shop-swiper">
			<shopBanner :detail="shopInfo" :isShare="isShare" />
		</view>
		<view>
			<view class="pd-lg fill-base flex-between rel" style="align-items: flex-end;">
				<view>
					<view class="f-sm-title text-bold c-black">{{shopInfo.name}}</view>
					<view class="pt-md c-warning" style="line-height: 1;">
						<text class="f-icontext">￥</text>
						<text class="f-sm-title text-bold">{{shopInfo.price}}</text>
					</view>
				</view>
				<!-- #ifdef MP-WEIXIN -->
				<button class="flex-center share-btn abs" open-type="share">
					<view class="iconfenxiang iconfont" style="font-size: 42rpx;" :style="{color:primaryColor}"></view>
					<text class="f-icontext c-caption" style="padding-left: 6rpx;">分享</text>
				</button>
				<!-- #endif -->
				<!-- #ifdef APP-PLUS -->
				<view class="flex-center share-btn abs" @tap.stop="toAppShare">
					<view class="iconfenxiang iconfont" style="font-size: 42rpx;" :style="{color:primaryColor}"></view>
					<text class="f-icontext c-caption" style="padding-left: 6rpx;">分享</text>
				</view>
				<!-- #endif -->
			</view>
			<view class="mt-md fill-base pl-lg pr-lg">
				<view class="f-mini-title c-black shop-box-title">商品详情</view>
				<view class="fill-base pt-lg pb-lg">
					<parser :html="shopInfo.desc" @linkpress="linkpress" show-with-animation lazy-load>加载中...</parser>
				</view>
				<!-- <rich-text :nodes="shopInfo.desc"></rich-text> -->
			</view>
		</view>
		<view class="space-max-footer"></view>
		<fix-bottom-button @confirm="$util.goUrl({url:shopInfo.phone ,openType:`call`})"
			:text="[{type:'confirm',text:'联系平台'}]" bgColor="#fff">
		</fix-bottom-button>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import parser from "@/components/jyf-Parser/index"
	// import shopBanner from "../../components/shop-banner"
	import shopBanner from "../../../technician/components/shop-banner.vue"
	import siteInfo from '@/siteinfo.js';
	export default {
		components: {
			parser,
			shopBanner
		},
		data() {
			return {
				isLoad: false,
				options: {},
				shopInfo: {},
				isShare: false
			}
		},
		computed: mapState({
			configInfo: state => state.config.configInfo,
			isGzhLogin: state => state.user.isGzhLogin,
		}),
		async onLoad(options) {
			let launch = 0
			// #ifdef MP-WEIXIN 
			let {
				scene
			} = await uni.getLaunchOptionsSync()
			launch = scene // 1154：朋友圈
			options.launch = launch
			// #endif
			options = await this.updateCommonOptions(options)
			let {
				pid = 0
			} = options
			options.pid = pid * 1
			if (pid) {
				this.isShare = true
			}
			this.options = options
			await this.initIndex()
			if (this.scanRecordId) {
				this.updateScanRecord()
			}
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onShareTimeline() {
			let {
				id: pid = 0
			} = this.userInfo
			let {
				id,
				name: title,
				cover: imageUrl
			} = this.shopInfo
			let query = `id=${id}&pid=${pid}`
			return {
				title,
				imageUrl,
				query
			}
		},
		onShareAppMessage() {
			let {
				id: pid = 0
			} = this.userInfo
			let {
				id,
				name: title,
				cover: imageUrl
			} = this.shopInfo
			let path = `/technician/pages/shop/detail?id=${id}&pid=${pid}`
			return {
				title,
				imageUrl,
				path
			}
		},
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo', 'addScanRecord',
				'updateScanRecord', 'updateCommonOptions'
			]),
			async initIndex(refresh = false) {
				let {
					pid = 0,
						launch = 0
				} = this.options
				let code = this.scanRecordId
				// #ifdef H5
				code = this.$util.getQueryString('code')
				// #endif
				if (pid && !code) {
					this.addScanRecord({
						type: 2,
						qr_id: pid,
						is_qr: 0
					})
				}
				let {
					id: uid = 0
				} = this.userInfo
				if (!this.configInfo.id || refresh || (pid && code && !uid)) {
					await this.getConfigInfo()
				}
				if (launch != 1154 && pid && !code && !uid) {
					await this.getUserInfo()
				}
				await this.goodsInfoCall()
				// #ifdef H5
				this.$jweixin.initJssdk(() => {
					this.toAppShare()
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.showShareMenu({
					menus: ['shareAppMessage', 'shareTimeline']
				})
				// #endif
			},
			initRefresh() {
				this.initIndex(true)
			},
			async goodsInfoCall() {
				this.$util.showLoading()
				let {
					id
				} = this.options
				this.shopInfo = await this.$api.technician.goodsInfo({
					id
				})
				this.isLoad = true
				this.$util.hideAll()
			},
			swiperChange(e) {},
			linkpress(res) {
				// #ifdef APP-PLUS
				if (/http/.test(res.href))
					this.$util.goUrl({
						url: res.href,
						openType: 'web'
					})
				// #endif
			},
			toAppShare() {
				let {
					id: pid = 0
				} = this.userInfo
				let {
					id,
					name: title,
					cover: imageUrl
				} = this.shopInfo
				let summary = ''
				let {
					siteroot
				} = siteInfo
				let url = siteroot.split('/index.php')[0]
				let href = `${url}/h5/#/technician/pages/shop/detail?id=${id}&pid=${pid}`

				// #ifdef H5
				this.$jweixin.showOptionMenu()
				this.$jweixin.shareAppMessage(title, summary, href, imageUrl)
				this.$jweixin.shareTimelineMessage(title, href, imageUrl)
				// #endif
				// #ifdef APP-PLUS
				let item = {
					href,
					title,
					summary,
					imageUrl,
				}
				if (this.configInfo.isIos) {
					this.$util.showLoading()
					this.toTransImg(item)
				} else {
					this.sharePage(item)
				}
				// #endif
			},
			toTransImg(item) {
				let that = this
				let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
				uni.downloadFile({
					url: item.imageUrl,
					success: (e) => {
						// 将png转换成jpg格式 只有jpg格式支持压缩api
						plus.zip.compressImage({
								src: e.tempFilePath,
								dst: `_doc/${cur_unix}.jpg`,
								format: "jpg"
							},
							response => {
								// 压缩图片
								plus.zip.compressImage({
									src: response.target,
									dst: `_doc/${cur_unix}-img.jpg`,
									quality: 50,
								}, res => {
									item.imageUrl = res.target || `/static/img/logo.png`
									that.sharePage(item)
								}, error => {
									item.imageUrl = `/static/img/logo.png`
									that.sharePage(item)
								})
							})
					}
				})
			},
			sharePage(item) {
				this.$util.hideAll()
				let {
					href,
					title,
					summary,
					imageUrl,
				} = item
				uni.share({
					provider: "weixin",
					scene: 'WXSceneSession',
					type: 0,
					href,
					title,
					summary,
					imageUrl,
					success: function(res) {
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.shop {
		.shop-swiper {
			height: 564rpx;
		}

		.shop-box-title {
			line-height: 110rpx;
			height: 110rpx;
		}

		.shop-box-item {
			width: 100%;
			height: 388rpx;

			image {
				vertical-align: bottom;
				width: 100%;
				height: 100%;
			}
		}

		.share-btn {
			right: 30rpx;
			bottom: 30rpx;
			height: 42rpx;
		}
	}
</style>